package org.jxf.gps.jfinal;




import java.sql.SQLException;

import org.jfk.core.controller.IndexController;
import org.jxf.gps.jfinal.controller.GpsController;
import org.jxf.gps.jfinal.controller.MonitorController;
import org.jxf.gps.jfinal.model.AlertInfo;
import org.jxf.gps.jfinal.model.Fence;
import org.jxf.gps.jfinal.model.GPSTerminal;
import org.jxf.gps.jfinal.model.MessageLog;
import org.jxf.gps.jfinal.model.Trace;
import org.jxf.gps.jfinal.model.Vehicle;
import org.jxf.gps.jt808.OnlineCheckPlugin;
import org.jxf.gps.jt808.server.ServerPlugin;
import org.jxf.gps.jt808.server.UDPServerPlugin;
import org.jxf.gps.jt808.server.WebSocketServerPlugin;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

public class MyJFinalConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		
		loadPropertyFile("db_config.properties");//加载数据库配置文件

		me.setDevMode(true);

		me.setViewType(ViewType.JSP);// 设置视图类型为Jsp，否则默认为FreeMarker
		
	}

	@Override
	public void configHandler(Handlers arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configInterceptor(Interceptors arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void configPlugin(Plugins me) {
		//me.add(new EhCachePlugin());
		
		// 配置C3p0数据库连接池插件
//		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
//		
//		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
//		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		
		
		// 配置druid数据库连接池插件
		DruidPlugin plugin = initDruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		
		me.add(plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(plugin);		
		arp.addMapping("trace", Trace.class);
		arp.addMapping("messageLog", MessageLog.class);
		arp.addMapping("gpsTerminal", GPSTerminal.class);
		arp.addMapping("vehicle", Vehicle.class);
		arp.addMapping("fence", Fence.class);
		arp.addMapping("alertinfo", AlertInfo.class);
		
		me.add(arp);
		arp.setShowSql(true);	
		
		ServerPlugin serverPlugin = new ServerPlugin();
		UDPServerPlugin udpServerPlugin = new UDPServerPlugin();
		OnlineCheckPlugin onlineCheckPlugin = new OnlineCheckPlugin();
		WebSocketServerPlugin webSocketServerPlugin = new WebSocketServerPlugin();
		me.add(serverPlugin);
		me.add(udpServerPlugin);		
		me.add(onlineCheckPlugin);
		me.add(webSocketServerPlugin);
	}

	@Override
	public void configRoute(Routes me) {
		
		me.add("/",IndexController.class);
		
		
		me.add("/gps", GpsController.class);
		me.add("/gpsmonitor", MonitorController.class);
	}
	
	/**
	 * init Druid
	 *
	 * @param url JDBC
	 * @param username 数据库用户
	 * @param password 数据库密码
	 * @return
	 */
	protected DruidPlugin initDruidPlugin(String url, String username, String password) {
		// 设置方言
		WallFilter wall = new WallFilter();
		String dbType = null;
		try {
			dbType = JdbcUtils.getDbType(url, JdbcUtils.getDriverClassName(url));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		wall.setDbType(dbType);

		DruidPlugin dp = new DruidPlugin(url, username, password);
		dp.addFilter(new StatFilter());
		dp.addFilter(wall);
		return dp;

	}	
}
